
//
//  LoginViewController.m
//  BeeQuick LoginActivity
//
//  Created by freak on 2016/11/19.
//  Copyright © 2016年 freak. All rights reserved.
//

#import "AXFLoginViewController.h"
#import "AXFWhiteNavView.h"
#import <SMSSDK/SMS_SDK/SMSSDK.h>
@interface AXFLoginViewController () <UITextFieldDelegate,AXFWhiteNavViewDelegate>
@property (nonatomic,weak) UIButton *loginButton;
@property (nonatomic,weak) UITextField *accountNumberText;
@property (nonatomic,weak) UITextField *passwordNumberText;
@property (nonatomic,weak)UIButton *voiceBtn;
@property (nonatomic,weak)UIButton *loginBtn;
@end

@implementation AXFLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.title = @"登录";
    self.view.backgroundColor = [UIColor whiteColor];
    [self login];
    
}
- (void)login
{
    
    AXFWhiteNavView *whiteNavView = [[AXFWhiteNavView alloc] init];
    
    [whiteNavView setTitleName:@"登录" andFunctionName:nil];
    
    whiteNavView.delegate = self;
    
    [self.view addSubview:whiteNavView];
    
    [whiteNavView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.offset(0);
        make.height.mas_equalTo(kNAVBARH + 20);
    }];
    
    //1. 输入账号
    UITextField *accountNumberText = [[UITextField alloc] init];
    accountNumberText.placeholder = @"请输入手机号";
    self.accountNumberText = accountNumberText;
    accountNumberText.delegate = self;
    
    [self.view addSubview:accountNumberText];
    [accountNumberText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(11);
        make.top.equalTo(whiteNavView.mas_bottom);
        make.width.mas_equalTo(200);
        make.height.mas_equalTo(31);
    }];
    
    //2.账号和密码之间的分割线
    UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 140, [UIScreen mainScreen].bounds.size.width, 1)];
    lineLabel.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:lineLabel];
    
    //3. 输入密码
    UITextField *passwordNumberText= [[UITextField alloc] init];
    passwordNumberText.placeholder = @"请输入验证码";
    [self.view addSubview:passwordNumberText];
    passwordNumberText.delegate = self;
    self.passwordNumberText = passwordNumberText;
    [passwordNumberText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(11);
        make.top.equalTo(lineLabel.mas_bottom).offset(16);
        make.width.mas_equalTo(200);
        make.height.mas_equalTo(31);
    }];
    
    // 4.密码下边的分割线
    UILabel *lineLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(10, 190, [UIScreen mainScreen].bounds.size.width, 1)];
    lineLabel2.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:lineLabel2];
    
    //5.获取验证码 按钮旁边的分割线
    UILabel *lineLabel3 = [[UILabel alloc] init];
    lineLabel3.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:lineLabel3];
    
    [lineLabel3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(1);
        make.height.mas_equalTo(30);
        make.left.equalTo(passwordNumberText.mas_right).offset(50);
        make.top.equalTo(lineLabel.mas_bottom).offset(10);
        make.bottom.equalTo(lineLabel2.mas_top).offset(-10);
    }];
    
    //6.获取验证码 按钮
    UIButton *getButton = [[UIButton alloc] init];
    [getButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    [getButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [getButton setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
    [self.view addSubview:getButton];
    [getButton addTarget:self action:@selector(sendVerificationCode:) forControlEvents:UIControlEventTouchUpInside];
    [getButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lineLabel.mas_bottom).offset(19);
        make.bottom.equalTo(lineLabel2.mas_top).offset(-19);
        make.left.equalTo(lineLabel3.mas_right).offset(10);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(10);
    }];
    
    
    
    //7.MARK: 登陆按钮
    UIButton *loginButton = [[UIButton alloc] init];
    self.loginBtn = loginButton;
    loginButton.enabled = NO;
    [loginButton setTitle:@"登录" forState:UIControlStateNormal];
    [loginButton setTitleColor:[UIColor cz_colorWithHex:0x999999] forState:UIControlStateNormal];
    loginButton.backgroundColor = [UIColor cz_colorWithHex:0xe8e8e8];
    
    [self.view addSubview:loginButton];
    
    [loginButton addTarget:self action:@selector(jumpToHome:) forControlEvents:UIControlEventTouchUpInside];
    
    [loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(10);
        make.right.offset(-10);
        make.top.equalTo(lineLabel2.mas_bottom).offset(60);
        make.height.mas_equalTo(40);
    }];
    
    // 关联
    _loginButton = loginButton;
    
    //8.底部label
    
    UILabel *hintLabel = [[UILabel alloc] init];
    [hintLabel setText:@"收不到短信？使用"];
    [hintLabel setFont:[UIFont systemFontOfSize:11]];
    [hintLabel setTextColor:[UIColor lightGrayColor]];
    [self.view addSubview:hintLabel];
    
    [hintLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(124);
        make.top.equalTo(loginButton.mas_bottom).offset(16);
        make.width.mas_equalTo(95);
        make.height.mas_equalTo(10);
    }];
    
    //9.获取验证码按钮
    
    UIButton *voiceButton = [[UIButton alloc] init];
    self.voiceBtn = voiceButton;
    [voiceButton setTitle:@"语音验证码" forState:UIControlStateNormal];
//    [voiceButton setTitle:@"已发送" forState:UIControlStateHighlighted];
    [voiceButton.titleLabel setFont:[UIFont systemFontOfSize:11]];
    [voiceButton setTitleColor:[UIColor cz_colorWithHex:0x70c0e6] forState:UIControlStateNormal];
    [voiceButton addTarget:self action:@selector(sendVoiceVerificationCode:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:voiceButton];
    
    [voiceButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(hintLabel.mas_right).offset(10);
        make.top.equalTo(loginButton.mas_bottom).offset(16);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(10);
    }];
    
}

- (void)sendVerificationCode:(UIButton *)sender
{
    [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:self.accountNumberText.text zone:@"86" customIdentifier:nil result:^(NSError *error) {
        if (!error) {
            NSLog(@"验证码发送成功");
        }else{
            NSLog(@"验证码发送失败---%@",error);
        }
    }];
    
    
    __block int timeout=30; //倒计时时间
    
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    
    dispatch_source_set_event_handler(_timer, ^{
        
        if(timeout<=0){ //倒计时结束，关闭
            
            dispatch_source_cancel(_timer);
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //设置界面的按钮显示 根据自己需求设置
                
                [sender setTitle:@"获取验证码" forState:UIControlStateNormal];
                
                sender.backgroundColor = [UIColor orangeColor];
                
                sender.userInteractionEnabled = YES;
                
            });
            
        }else{
            
            //            int minutes = timeout / 60;
            
            int seconds = timeout % 59;
            
            NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //设置界面的按钮显示 根据自己需求设置
                
                NSLog(@"____%@",strTime);
                
                [sender setTitle:[NSString stringWithFormat:@"%@",strTime] forState:UIControlStateNormal];
                
                sender.backgroundColor = [UIColor grayColor];
                
                sender.userInteractionEnabled = NO;
                
            });
            
            timeout--;
            
        }
        
    });
    
    dispatch_resume(_timer);
    
}



- (void)sendVoiceVerificationCode:(UIButton *)sender
{
    [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodVoice phoneNumber:self.accountNumberText.text zone:@"86" customIdentifier:nil result:^(NSError *error) {
        if (!error) {
            NSLog(@"验证码发送成功");
            [self.voiceBtn setTitle:@"已发送" forState:UIControlStateNormal];
        }else{
            NSLog(@"验证码发送失败---%@",error);
        }
    }];
}

- (void)sendSucceed
{
    
    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(150, 280,80,80)];
    
    label.backgroundColor = [UIColor cz_colorWithHex:0xe8e8e8];
    
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:12];
    
    label.text = @"发送成功√";
    [self.view addSubview:label];
    CATransition * transion = [CATransition animation];
    
    transion.type = kCATransitionFade;
    transion.subtype = 0;
    [label.layer addAnimation:transion forKey:nil];//给Label.layer 添加动画 //设置延时效果
    
    //不占用主线程
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(),^{
        
        [label removeFromSuperview];
        
    });//1.5秒后，把label移出视图
}

// 文本框的代理方法
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    if (textField == self.passwordNumberText) {
        if (self.accountNumberText.text.length > 0) {
            self.loginButton.backgroundColor = [UIColor cz_colorWithHex:0xffd600];
        }else{
            self.loginButton.backgroundColor = [UIColor cz_colorWithHex:0xe8e8e8];
        }
    }
    if (textField == self.accountNumberText) {
        if (self.passwordNumberText.text.length > 0) {
            self.loginButton.backgroundColor = [UIColor cz_colorWithHex:0xffd600];
        }else{
            self.loginButton.backgroundColor = [UIColor cz_colorWithHex:0xe8e8e8];
        }
    }
    
    return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (textField == self.passwordNumberText) {
        self.loginButton.enabled = YES;
    }
}


// 假登录跳转首页
- (void)jumpToHome:(UIButton*)sender
{
    [self.navigationController popToRootViewControllerAnimated:YES];
    
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"pushToHome" object:nil];
}

- (void)backToViewController
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
